<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogStatus==='create'?'新增':'编辑'"
    width="600px"
  >
    <el-form 
      ref="articleFormRef"
      :model="tableItem" 
      :rules="rules"
      label-position="top"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="tableItem.title" />
      </el-form-item>

      <el-form-item label="类别" prop="category">
        <el-select v-model="tableItem.category" placeholder="选择一个类别">
          <el-option label="分类1" value="分类1" />
          <el-option label="分类2" value="分类2" />
        </el-select>
      </el-form-item>

      <el-form-item label="作者" prop="author">
        <el-input v-model="tableItem.author" />
      </el-form-item>

      <el-form-item label="发布时间" prop="createTime">
        <el-date-picker
          v-model="tableItem.createTime"
          type="date"
          placeholder="选择一个日期"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { createArticle, updateArticle } from '@/api/article'
import { parseTime } from '@/utils'
import { validate } from '@/utils/validate'
// const props = defineProps({
//   selectItem: {
//     type: Array,
//     default: () => [],
//   },
// })
const tableItem = reactive({
  id: null,
  title: '',
  category: '',
  author: '',
  createTime: '',
})

const rules = reactive({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  category: [{ required: true, message: '请选择类别', trigger: 'blur' }],
  author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
  createTime: [{ required: true, message: '请选择时间', trigger: 'blur' }],
})

const dialogVisible = ref(false)
const dialogStatus = ref('create')

const reset = () => {
  tableItem.id = null
  tableItem.title = ''
  tableItem.category = ''
  tableItem.author = ''
  tableItem.createTime = ''
}

const articleFormRef = ref(null)

const emit = defineEmits(['listChange'])

const createData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    const params = JSON.parse(JSON.stringify(tableItem))
    params.id = parseInt(Math.random() * 100) + 1024
    console.log('params.id', params.id)
    params.createTime = parseTime(params.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
    createArticle(params)
    .then(() => {
      // TODO use res data
      emit('listChange')
      reset()
      dialogVisible.value = false
      ElMessage.success('创建成功！')
    })
    .catch(() => {
      reset()
      dialogVisible.value = false
      ElMessage.error('创建失败！')
    })
  }
}

const updateData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    const params = JSON.parse(JSON.stringify(tableItem))
    updateArticle(params)
    .then(() => {
      // params.createTime = parseTime(params.createTime, '{y}-{m}-{d}')
      emit('listChange')
      reset()
      dialogVisible.value = false
      ElMessage.success('更新成功！')
    })
    .catch(() => {
      reset()
      dialogVisible.value = false
      ElMessage.error('更新失败！')
    })
  }
}

defineExpose({ dialogVisible, tableItem, dialogStatus, reset })

</script>
<style lang="scss" scoped>
</style>
